<template>
  <div class="app-container">
    <el-dialog :title="title" :visible.sync="open" :width="dialogWidth" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="85px">
        <el-form-item label="合同编码" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入合同编码"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="合同标题" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入合同标题"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="发票状态" prop="invoiceState">
          <el-select v-model="queryParams.invoiceState" placeholder="请选择发票状态" clearable size="small">
            <el-option
              v-for="dict in stateOptions.invoiceState"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="合同状态" prop="state">
          <el-select v-model="queryParams.state" placeholder="请选择合同状态" clearable size="small">
            <el-option
              v-for="dict in stateOptions.contractState"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table ref="contractMainList" v-loading="loading" :data="mainList"  @selection-change="handleSelectionChange" @row-click="handleRowClick" highlight-current-row>
        <el-table-column label="主键ID" align="center" prop="gid" v-if="false"/>
        <el-table-column label="合同编码" align="center" prop="code" width="230px"/>
        <el-table-column label="合同名称" align="center" prop="name" />
        <el-table-column label="合同类型" align="center" prop="typeFlag" width="95px" :formatter="contractTypeFormat"/>
        <el-table-column label="发票状态" align="center" prop="invoiceState" width="95px" :formatter="invoiceStateFormat"/>
        <el-table-column label="已收款金额" align="center" prop="receivedAmount" width="115px"/>
        <el-table-column label="合同金额" align="center" prop="contractAmount" width="115px"/>
        <el-table-column label="合同状态" align="center" prop="state" width="85px" :formatter="contractStateFormat" />
        <el-table-column label="操作" align="center" width="85px"  class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini" type="text"
              icon="el-icon-copy-document"
              @click="selectItem(scope.row)"
              v-hasPermi="['contract:main:edit']"
            >带回</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listMain, getMain} from "@/api/contract/main";
import {getDictByUrl} from "@/api/system/dict/data";

export default {
  name: "Main",
  components: {
  },props:{
    contractOpen: false,
    dialogWidth: {
      type:String,
      default:'1850px'
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同管理表格数据
      mainList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open:false,
      // 合同状态字典
      stateOptions: [],
      selfDictData: {},
      // 查询参数
      queryParams: {
        gid:null,
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        typeFlag: null,
        invoiceState: null,
        receivedAmount: null,
        contractAmount: null,
        state: null,
        signDeptId: null,
        signUserId: null,
        parentId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "合同编码不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "合同名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  beforeCreate(){
    this.getDicts("contract_bill_state").then(response => {
      this.stateOptions.contractState = response.data;
    });
    this.getDicts("contract_bill_type").then(response => {
      this.stateOptions.contractType = response.data;
    });
    this.getDicts("invoice_state").then(response => {
      this.stateOptions.invoiceState = response.data;
    });
    getDictByUrl("/common/tool/createUserDict").then(response => {
      this.selfDictData.userDict = response;
    });
    getDictByUrl("/common/tool/createDeptDict").then(response => {
      this.selfDictData.deptDict = response;
    });
  },
  created() {
    this.getList();

  },
  methods: {
    getRowByGid(gid){
      getMain(gid).then(response => {
        this.$emit('selectContract',response.data);
      });
    },
    //选择带回
    selectItem(row){
      this.$emit('selectContract',row);
    },
    /** 查询合同管理列表 */
    getList() {
      this.loading = true;
      listMain(this.queryParams).then(response => {
        this.mainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    formatUser(row,col){
      return this.selfDictData.userDict[String(row.signUserId)]+''
    },
    formatDept(row,col){
      return this.selfDictData.deptDict[String(row.signDeptId)]+''
    },
    // 合同状态字典翻译
    contractStateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions.contractState, row.state);
    },
    //合同类型字典翻译
    contractTypeFormat(row, column) {
      return this.selectDictLabel(this.stateOptions.contractType, row.typeFlag);
    },
    //发票状态字典翻译
    invoiceStateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions.invoiceState, row.invoiceState);
    },
    //点击行触发，选中或不选中复选框
    handleRowClick(row) {
      this.$refs.contractMainList.toggleRowSelection(row);
      const _selectData = this.$refs.contractMainList.selection;
      this.handleSelectionChange(_selectData);
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    }
  },watch:{
    "contractOpen"(val){
      this.open=val;
    },
    "open"(val){
      this.$emit('showContractStateSync',val);
    }
  }
};
</script>

